package com.example.zyf.sortlistviewandsidebar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import java.util.List;

/**
 * Created by zyf on 2016/3/28.
 */
public class SortAdapter extends BaseAdapter implements SectionIndexer {

    Context context;
    List<City> cities;

    public SortAdapter(Context context, List<City> cities) {
        this.context = context;
        this.cities = cities;
    }



    @Override
    public int getCount() {
        return cities.size();
    }

    @Override
    public Object getItem(int position) {
        return cities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.sort_listview_dialog_item, null);
            viewHolder = new ViewHolder();
            viewHolder.catalog = (TextView) convertView.findViewById(R.id.catalog);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        /**
         * 根据位置获取首字母，ascii
         */
        int section = getSectionForPosition(position);
        /**
         * 根据首字母，获取第一个与这个首字母相同的项目所在的位置
         */
        if (position == getPositionForSection(section)) {
            viewHolder.catalog.setVisibility(View.VISIBLE);
            viewHolder.catalog.setText(String.valueOf(cities.get(position).getCityInitial()));
        } else {
            viewHolder.catalog.setVisibility(View.GONE);
        }
        viewHolder.title.setText(cities.get(position).getName());

        return convertView;
    }

    static class ViewHolder {
        TextView catalog;
        TextView title;
    }

    @Override
    public Object[] getSections() {
        return new Object[0];
    }

//    for (int i = 0; i < getCount(); i++) {
//        String sortStr = list.get(i).getCityInitial();
//        char firstChar = sortStr.toUpperCase().charAt(0);
//        if (firstChar == section) {
//            return i;
//        }
//    }
//
//    return -1;
    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i=0;i<getCount();i++){
            char cityInitial=cities.get(i).getCityInitial();
            if (cityInitial==sectionIndex){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
//        return CharacterParser.getInstance().getSelling(citys[position]).charAt(0);
        return cities.get(position).getCityInitial();
    }
}
